package com.canyou.listener;

import com.canyou.event.UserLoginEvent;
import com.canyou.system.service.LoginInfoService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.ApplicationListener;
import org.springframework.scheduling.annotation.Async;
import org.springframework.stereotype.Component;

/**
 * @author cd
 */
@Slf4j
@Component
public class UserLoginListener implements ApplicationListener<UserLoginEvent> {

    @Autowired
    private LoginInfoService loginInfoService;

    @Async
    @Override
    public void onApplicationEvent(UserLoginEvent userLoginEvent) {
        try {
            loginInfoService.saveLoginInfo(userLoginEvent.getRequest(), userLoginEvent.getLoginStatus(),
                    userLoginEvent.getErrorMsg(), userLoginEvent.getUserName());
        } catch (Exception e) {
            log.error("创建登录日志失败异常!", e);
        }
    }
}
